package com.lzs.adx.web.modules.adx.controller;


import com.alibaba.fastjson.JSONObject;
import com.lzs.adx.commmon.constant.Constant;
import com.lzs.adx.commmon.page.PageData;
import com.lzs.adx.commmon.utils.Result;
import com.lzs.adx.commmon.validator.AssertUtils;
import com.lzs.adx.commmon.validator.ValidatorUtils;
import com.lzs.adx.commmon.validator.group.AddGroup;
import com.lzs.adx.commmon.validator.group.DefaultGroup;
import com.lzs.adx.commmon.validator.group.UpdateGroup;
import com.lzs.adx.web.commmon.annotation.LogOperation;
import com.lzs.adx.web.commmon.utils.ExcelUtils;
import com.lzs.adx.web.modules.adx.dto.AccountDTO;
import com.lzs.adx.web.modules.adx.dto.ReportDTO;
import com.lzs.adx.web.modules.adx.entity.ResultTypeEntity;
import com.lzs.adx.web.modules.adx.excel.ReportExcel;
import com.lzs.adx.web.modules.adx.service.AccountService;
import com.lzs.adx.web.modules.adx.service.ReportService;
import com.lzs.adx.web.modules.adx.service.ResultTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.compress.utils.Lists;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * 广告上报表
 *
 * @author wangzhijun 825400558@qq.com
 * @since 1.0.0 2023-06-06
 */
@RestController
@RequestMapping("adx/report")
@Api(tags="广告上报表")
public class ReportController {
    @Autowired
    private ReportService reportService;

    @Autowired
    private AccountService accountService;

    @Autowired
    private ResultTypeService resultTypeService;

    public static final String baseUrlApi="https://capi.qgxf.com.cn/";

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    @RequiresPermissions("adx:report:page")
    public Result<PageData<ReportDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        Object dspId = params.get("dspId");
        PageData<ReportDTO> page;
        if(params.containsKey("dspId")) {
             page = reportService.page(params);
        }else{
            page=new PageData<>(Lists.newArrayList(),0);
        }

        return new Result<PageData<ReportDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("adx:report:info")
    public Result<ReportDTO> get(@PathVariable("id") Long id){
        ReportDTO data = reportService.get(id);

        return new Result<ReportDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("adx:report:save")
    public Result save(@RequestBody ReportDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        reportService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("adx:report:update")
    public Result update(@RequestBody ReportDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        reportService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("adx:report:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        reportService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("adx:report:export")
    public Result export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<ReportDTO> list = reportService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, ReportExcel.class);
        return Result.ok();
    }

    public  Result test(String dspId,String type){
        AccountDTO detail = accountService.getDetail(dspId);
        //回传类型
        if(Objects.isNull(detail.getDspExtend())){
            return Result.errorReq(-1,"未配置回传字段，不支持手动页面回传");
        }
        //不是V2的版本，没办法做回传
        if(!detail.getDspCallbackUrl().contains("v2/base/callback")){

            return Result.errorReq(-2,"不是V2暂不支持页面手动回传");
        }

        JSONObject jsonObject = JSONObject.parseObject(detail.getDspExtend());
        if(jsonObject.containsKey("eventType")){
            ResultTypeEntity resultTypeEntity= resultTypeService.get(detail.getProductChannel(),type);
            String  eventType= jsonObject.getString("eventType");
            String finalType=eventType+"="+resultTypeEntity.getResultType();



        }else{

        }
        return  Result.ok();
    }

}